package com.mhkj.base.busiType.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.mhkj.base.busiType.dao.BusinessTypeDao;
import com.mhkj.base.busiType.entity.BusinessType;
import com.mhkj.base.busiType.service.BusinessTypeService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;

/**
 * 业务类型逻辑实现类
 * 
 * @author ypx
 * @date 2018年7月2日
 * @version 1.0
 */
@Service
@Transactional
public class BusinessTypeServiceImpl extends BaseServiceImpl<BusinessType> implements BusinessTypeService {

	@Autowired
	private BusinessTypeDao businessTypeDao;
	@Resource
	private OrgService orgService;
	
	@Autowired
	public void setBusinessTypeDao(BusinessTypeDao businessTypeDao) {
		super.setBaseRepository(businessTypeDao);
	}

	/**
	 * 分页查询业务类型
	 */
	@Override
	public Pagination<BusinessType> findPage(BusinessType entity, Integer pageIndex, Integer pageSize) {

		Specification<BusinessType> spec = new Specification<BusinessType>() {
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<BusinessType> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

				if (!StringUtils.isEmpty(entity.getTypeName())) {
					lstPredicates.add(criteriaBuilder.like(root.get("typeName").as(String.class),
							"%" + entity.getTypeName() + "%"));
				}
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		PageRequest pageable = PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id");
		Page<BusinessType> pageT = businessTypeDao.findAll(spec, pageable);
		Pagination<BusinessType> pagination = new Pagination<BusinessType>();
		pagination.setTotal(businessTypeDao.count(spec));
		List<BusinessType> businessTypes = pageT.getContent();
		for(BusinessType businessType:businessTypes){
			Employee  employee = orgService.employeeInfo(businessType.getCreateBy());
			businessType.setCreateName(employee.getName());
		}
		pagination.setData(businessTypes);
		return pagination;
	}

	/**
	 * 查询业务类型和业务编号是否已经存在
	 * 
	 * @param typeCoding
	 * @param typeName
	 * @return
	 */
	@Override
	public String isWhereByName(String typeCoding, String typeName) {

		Specification<BusinessType> spec = new Specification<BusinessType>() {
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<BusinessType> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

				if (typeCoding != null) {
					Predicate p2 = criteriaBuilder.equal(root.get("typeCoding").as(String.class), typeCoding);
					lstPredicates.add(p2);
				}

				if (typeName != null) {
					Predicate p2 = criteriaBuilder.equal(root.get("typeName").as(String.class), typeName);
					lstPredicates.add(p2);
				}

				return criteriaBuilder.or(lstPredicates.toArray(arrayPredicates));
			}
		};
		List<BusinessType> findAll = businessTypeDao.findAll(spec);
		if (findAll.size() == 0) {
			return "ok";
		} else {
			return "该项目编号或者类型已经存在，请重新输入";
		}

	}

	@Override
	public BusinessType getByName(String name) {
		return businessTypeDao.getByName(name);
	}
}